package com.hdw.task.job;

import com.hdw.common.base.api.BaseMsgService;
import com.hdw.common.constant.CommonConstant;
import com.hdw.common.util.DateUtil;
import com.hdw.task.api.JobExecService;
import com.hdw.task.core.model.XxlJobLog;
import com.hdw.task.mapper.XxlJobLogMapper;
import com.hdw.task.service.JobExecServiceImpl;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by liujunlei on 2021/6/7.
 */
@Component
public class JobHandler {

    private static Logger logger = LoggerFactory.getLogger(JobHandler.class);
    private String nowDate;
    @Autowired
    private JobExecService jobExecService;
    @Autowired
    XxlJobLogMapper xxlJobLogDao;
    @Autowired
    private BaseMsgService baseMsgService;

    /**
     * 就以30为界，30天之内的只存储，30+1的实现统计。30是个定义变量如何。
     * 现场沟通病案的更新问题，如果更新没有明确标志，数据的准确性会存疑
     */
    @XxlJob("dayBadataXxljob")
    public void dayBadataXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("dayBadataXxljob被调用"+param);
        // default success
    }

    /**
     *  按天抽取基础数据
     *  hos->ods->dw->dm
     */
    @XxlJob("dayBasedataXxljob")
    public void dayBasedataXxljob() throws Exception {
        XxlJobLog jobLog = new XxlJobLog();
        jobLog.setJobId(Integer.parseInt(XxlJobHelper.getJobId()+""));
        jobLog.setHandleCode(0);
        List<XxlJobLog> list = xxlJobLogDao.findByModel(jobLog);
        if(list==null) {
            return;
        }
        jobLog = list.get(0);
        Long jobLogId = jobLog.getId();
        String startDate="",endDate; //格式为20200111
        if(StringUtils.isNotBlank(nowDate)) {
            endDate = nowDate;
        } else {
            endDate = DateUtil.toDateStrByFormat(new Date(),DateUtil.yyyyMMdd);
        }
        try {
            Date yesterday = DateUtil.calculate(DateUtil.toDateByFormat(endDate,DateUtil.yyyyMMdd), Calendar.DAY_OF_MONTH, -1);
            startDate = DateUtil.toDateStrByFormat(yesterday,DateUtil.yyyyMMdd);
            boolean isSuccess = jobExecService.dayBasedataExec(startDate,endDate,jobLogId);
            if(!isSuccess) {
                XxlJobHelper.handleFail();
            }
        }catch (Exception e) {
            baseMsgService.insert(CommonConstant.MSG_CODE_C01,"["+startDate+"~"+endDate+"]按天抽取基础数据失败","数据需要重抽");
            XxlJobHelper.handleFail();
        }
    }

    /**
     *  按天检测更新
     *  更新场景：
     *   1. 取消出院
            select *from 住院记录 where 取消标志=true and 取消出院时间='';
            跟事实表dbc_inp相关的基础指标，->复合指标执行数据更新
         2. 非当天退号
     */
    @XxlJob("dayChkUpdateXxljob")
    public void dayChkUpdateXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("dayChkUpdateXxljob被调用"+param);
        // default success
    }

    /**
     *  按分钟抽取实时数据
     */
    @XxlJob("minHosdataXxljob")
    public void minHosdataXxljob() throws Exception {
        Date triggerTime = new Date();
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();
        //etlCommonService.addOrgJobParameter(parametersBuilder);
        //JobUtil.addDayParameter(nowDate,parametersBuilder);
        //etlRunService.runEtlBaseJobByDay(nowDate,"minToDm","minHosdataXxljob",triggerTime);
    }

    /**
     *  按小时抽取实时数据
     */
    @XxlJob("hourHosdataXxljob")
    public void hourHosdataXxljob() throws Exception {
        String param = XxlJobHelper.getJobParam();
        System.out.println("hourHosdataXxljob被调用"+param);
        // default success
    }
}
